home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / MDB / Modules / Manager / oci8.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  18.5 KB  |  384 lines

  1. <?php 
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB is a merge of PEAR DB and Metabases that provides a unified DB   |
  10. // | API as well as database abstraction for PHP applications.            |
  11. // | This LICENSE is in the BSD license style.                            |
  12. // |                                                                      |
  13. // | Redistribution and use in source and binary forms, with or without   |
  14. // | modification, are permitted provided that the following conditions   |
  15. // | are met:                                                             |
  16. // |                                                                      |
  17. // | Redistributions of source code must retain the above copyright       |
  18. // | notice, this list of conditions and the following disclaimer.        |
  19. // |                                                                      |
  20. // | Redistributions in binary form must reproduce the above copyright    |
  21. // | notice, this list of conditions and the following disclaimer in the  |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // |                                                                      |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission.                                                  |
  28. // |                                                                      |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@backendmedia.com>                         |
  43. // +----------------------------------------------------------------------+
  44.  
  45. // $Id: oci8.php,v 1.7.4.2 2004/01/08 13:43:01 lsmith Exp $
  46.  
  47. if (!defined('MDB_MANAGER_OCI8_INCLUDED')) {
  48.     define('MDB_MANAGER_OCI8_INCLUDED', 1);
  49.  
  50. require_once('MDB/Modules/Manager/Common.php');
  51.  
  52. /**
  53.  * MDB oci8 driver for the management modules
  54.  * 
  55.  * @package MDB
  56.  * @category Database
  57.  * @author Lukas Smith <smith@backendmedia.com> 
  58.  */
  59. class MDB_Manager_oci8 extends MDB_Manager_Common {
  60.  
  61.     // {{{ createDatabase()
  62.  
  63.     /**
  64.      * create a new database
  65.      * 
  66.      * @param object $dbs database object that is extended by this class
  67.      * @param string $name name of the database that should be created
  68.      * @return mixed MDB_OK on success, a MDB error on failure
  69.      * @access public 
  70.      */
  71. /*
  72.     function createDatabase(&$db, $name)
  73.     {
  74.         $user = $db->getOption('DBAUser');
  75.         if (MDB::isError($user)) {
  76.             return($db->raiseError(MDB_ERROR_INSUFFICIENT_DATA, NULL, NULL, 'Create database',
  77.                 'it was not specified the Oracle DBAUser option'));
  78.         }
  79.         $password = $db->getOption('DBAPassword');
  80.         if (MDB::isError($password)) {
  81.             return($db->raiseError(MDB_ERROR_INSUFFICIENT_DATA, NULL, NULL, 'Create database',
  82.                 'it was not specified the Oracle DBAPassword option'));
  83.         }
  84.         if (!MDB::isError($result = $db->connect($user, $password, 0))) {
  85.             $tablespace = $db->getOption('DefaultTablespace');
  86.             if(MDB::isError($tablespace)) {
  87.                 $tablespace = '';
  88.             } else {
  89.                 $tablespace = ' DEFAULT TABLESPACE '.$tablespace;
  90.             }
  91.             if (!MDB::isError($result = $db->_doQuery('CREATE USER '.$db->user.' IDENTIFIED BY '.$db->password.$tablespace))) {
  92.                 if (!MDB::isError($result = $db->_doQuery('GRANT CREATE SESSION, CREATE TABLE,UNLIMITED TABLESPACE,CREATE SEQUENCE TO '.$db->user))) {
  93.                     return(MDB_OK);
  94.                 } else {
  95.                     if (MDB::isError($result2 = $db->_doQuery('DROP USER '.$db->user.' CASCADE'))) {
  96.                         return($db->raiseError(MDB_ERROR, '','', 'Create database',
  97.                             'could not setup the database user ('.$result->getUserinfo().') and then could drop its records ('.$result2->getUserinfo().')'));
  98.                     }
  99.                     return($db->raiseError(MDB_ERROR, '','', 'Create database',
  100.                         'could not setup the database user ('.$result->getUserinfo().')'));
  101.                 }
  102.             }
  103.         }
  104.         return($result);
  105.     }
  106. */
  107.  
  108.     // }}}
  109.     // {{{ dropDatabase()
  110.  
  111.     /**
  112.      * drop an existing database
  113.      * 
  114.      * @param object $dbs database object that is extended by this class
  115.      * @param string $name name of the database that should be dropped
  116.      * @return mixed MDB_OK on success, a MDB error on failure
  117.      * @access public 
  118.      */
  119. /*
  120.     function dropDatabase(&$db, $name)
  121.     {
  122.         $user = $db->getOption('DBAUser');
  123.         if (MDB::isError($user)) {
  124.             return($db->raiseError(MDB_ERROR_INSUFFICIENT_DATA, NULL, NULL, 'Create database',
  125.                 'it was not specified the Oracle DBAUser option'));
  126.         }
  127.         $password = $db->getOption('DBAPassword');
  128.         if (MDB::isError($password)) {
  129.             return($db->raiseError(MDB_ERROR_INSUFFICIENT_DATA, NULL, NULL, 'Create database',
  130.                 'it was not specified the Oracle DBAPassword option'));
  131.         }
  132.         if (MDB::isError($db->connect($user, $password, 0))) {
  133.             return($result);
  134.         }
  135.         return($db->_doQuery('DROP USER '.$db->user.' CASCADE'));
  136.     }
  137. */
  138.  
  139.     // }}}
  140.     // {{{ alterTable()
  141.  
  142.     /**
  143.      * alter an existing table
  144.      * 
  145.      * @param object $dbs database object that is extended by this class
  146.      * @param string $name name of the table that is intended to be changed.
  147.      * @param array $changes associative array that contains the details of each type
  148.      *                              of change that is intended to be performed. The types of
  149.      *                              changes that are currently supported are defined as follows:
  150.      * 
  151.      *                              name
  152.      * 
  153.      *                                 New name for the table.
  154.      * 
  155.      *                             AddedFields
  156.      * 
  157.      *                                 Associative array with the names of fields to be added as
  158.      *                                  indexes of the array. The value of each entry of the array
  159.      *                                  should be set to another associative array with the properties
  160.      *                                  of the fields to be added. The properties of the fields should
  161.      *                                  be the same as defined by the Metabase parser.
  162.      * 
  163.      *                                 Additionally, there should be an entry named Declaration that
  164.      *                                  is expected to contain the portion of the field declaration already
  165.      *                                  in DBMS specific SQL code as it is used in the CREATE TABLE statement.
  166.      * 
  167.      *                             RemovedFields
  168.      * 
  169.      *                                 Associative array with the names of fields to be removed as indexes
  170.      *                                  of the array. Currently the values assigned to each entry are ignored.
  171.      *                                  An empty array should be used for future compatibility.
  172.      * 
  173.      *                             RenamedFields
  174.      * 
  175.      *                                 Associative array with the names of fields to be renamed as indexes
  176.      *                                  of the array. The value of each entry of the array should be set to
  177.      *                                  another associative array with the entry named name with the new
  178.      *                                  field name and the entry named Declaration that is expected to contain
  179.      *                                  the portion of the field declaration already in DBMS specific SQL code
  180.      *                                  as it is used in the CREATE TABLE statement.
  181.      * 
  182.      *                             ChangedFields
  183.      * 
  184.      *                                 Associative array with the names of the fields to be changed as indexes
  185.      *                                  of the array. Keep in mind that if it is intended to change either the
  186.      *                                  name of a field and any other properties, the ChangedFields array entries
  187.      *                                  should have the new names of the fields as array indexes.
  188.      * 
  189.      *                                 The value of each entry of the array should be set to another associative
  190.      *                                  array with the properties of the fields to that are meant to be changed as
  191.      *                                  array entries. These entries should be assigned to the new values of the
  192.      *                                  respective properties. The properties of the fields should be the same
  193.      *                                  as defined by the Metabase parser.
  194.      * 
  195.      *                                 If the default property is meant to be added, removed or changed, there
  196.      *                                  should also be an entry with index ChangedDefault assigned to 1. Similarly,
  197.      *                                  if the notNULL constraint is to be added or removed, there should also be
  198.      *                                  an entry with index ChangedNotNull assigned to 1.
  199.      * 
  200.      *                                 Additionally, there should be an entry named Declaration that is expected
  201.      *                                  to contain the portion of the field changed declaration already in DBMS
  202.      *                                  specific SQL code as it is used in the CREATE TABLE statement.
  203.      *                             Example
  204.      *                                 array(
  205.      *                                     'name' => 'userlist',
  206.      *                                     'AddedFields' => array(
  207.      *                                         'quota' => array(
  208.      *                                             'type' => 'integer',
  209.      *                                             'unsigned' => 1
  210.      *                                             'Declaration' => 'quota INT'
  211.      *                                         )
  212.      *                                     ),
  213.      *                                     'RemovedFields' => array(
  214.      *                                         'file_limit' => array(),
  215.      *                                         'time_limit' => array()
  216.      *                                         ),
  217.      *                                     'ChangedFields' => array(
  218.      *                                         'gender' => array(
  219.      *                                             'default' => 'M',
  220.      *                                             'ChangeDefault' => 1,
  221.      *                                             'Declaration' => "gender CHAR(1) DEFAULT 'M'"
  222.      *                                         )
  223.      *                                     ),
  224.      *                                     'RenamedFields' => array(
  225.      *                                         'sex' => array(
  226.      *                                             'name' => 'gender',
  227.      *                                             'Declaration' => "gender CHAR(1) DEFAULT 'M'"
  228.      *                                         )
  229.      *                                     )
  230.      *                                 )
  231.      * @param boolean $check indicates whether the function should just check if the DBMS driver
  232.      *                              can perform the requested table alterations if the value is TRUE or
  233.      *                              actually perform them otherwise.
  234.      * @access public 
  235.      * @return mixed MDB_OK on success, a MDB error on failure
  236.      */
  237.     function alterTable(&$db, $name, $changes, $check)
  238.     {
  239.         if ($check) {
  240.             for($change = 0, reset($changes);
  241.                 $change < count($changes);
  242.                 next($changes), $change++)
  243.             {
  244.                 switch (key($changes)) {
  245.                     case 'AddedFields':
  246.                     case 'RemovedFields':
  247.                     case 'ChangedFields':
  248.                     case 'name':
  249.                         break;
  250.                     case 'RenamedFields':
  251.                     default:
  252.                         return($db->raiseError(MDB_ERROR, NULL, NULL, 'Alter table',
  253.                             'change type "'.key($changes).'" not yet supported'));
  254.                 }
  255.             }
  256.             return(MDB_OK);
  257.         } else {
  258.             if (isset($changes['RemovedFields'])) {
  259.                 $query = ' DROP (';
  260.                 $fields = $changes['RemovedFields'];
  261.                 for($field = 0, reset($fields);
  262.                     $field < count($fields);
  263.                     next($fields), $field++)
  264.                 {
  265.                     if ($field > 0) {
  266.                         $query .= ', ';
  267.                     }
  268.                     $query .= key($fields);
  269.                 }
  270.                 $query .= ')';
  271.                 if (MDB::isError($result = $db->query("ALTER TABLE $name $query"))) {
  272.                     return($result);
  273.                 }
  274.                 $query = '';
  275.             }
  276.             $query = (isset($changes['name']) ? 'RENAME TO '.$changes['name'] : '');
  277.             if (isset($changes['AddedFields'])) {
  278.                 $fields = $changes['AddedFields'];
  279.                 for($field = 0, reset($fields);
  280.                     $field < count($fields);
  281.                     next($fields), $field++)
  282.                 {
  283.                     $query .= ' ADD ('.$fields[key($fields)]['Declaration'].')';
  284.                 }
  285.             }
  286.             if (isset($changes['ChangedFields'])) {
  287.                 $fields = $changes['ChangedFields'];
  288.                 for($field = 0, reset($fields);
  289.                     $field < count($fields);
  290.                     next($fields), $field++)
  291.                 {
  292.                     $current_name = key($fields);
  293.                     if (isset($renamed_fields[$current_name])) {
  294.                         $field_name = $renamed_fields[$current_name];
  295.                         unset($renamed_fields[$current_name]);
  296.                     } else {
  297.                         $field_name = $current_name;
  298.                     }
  299.                     $change = '';
  300.                     $change_type = $change_default = 0;
  301.                     if (isset($fields[$current_name]['type'])) {
  302.                         $change_type = $change_default = 1;
  303.                     }
  304.                     if (isset($fields[$current_name]['length'])) {
  305.                         $change_type = 1;
  306.                     }
  307.                     if (isset($fields[$current_name]['ChangedDefault'])) {
  308.                         $change_default = 1;
  309.                     }
  310.                     if ($change_type) {
  311.                         $change .= ' '.$db->getTypeDeclaration($fields[$current_name]['Definition']);
  312.                     }
  313.                     if ($change_default) {
  314.                         $change .= ' DEFAULT '.(isset($fields[$current_name]['Definition']['default']) ? $db->getFieldValue($fields[$current_name]['Definition']['type'], $fields[$current_name]['Definition']['default']) : 'NULL');
  315.                     }
  316.                     if (isset($fields[$current_name]['ChangedNotNull'])) {
  317.                         $change .= (isset($fields[$current_name]['notnull']) ? ' NOT' : '').' NULL';
  318.                     }
  319.                     if (strcmp($change, '')) {
  320.                         $query .= " MODIFY ($field_name$change)";
  321.                     }
  322.                 }
  323.             }
  324.             if($query != '' && MDB::isError($result = $db->query("ALTER TABLE $name $query"))) {
  325.                 return($result);
  326.             }
  327.             return(MDB_OK);
  328.         }
  329.         return($db->raiseError());
  330.     }
  331.  
  332.     // }}}
  333.     // {{{ listDatabases()
  334.  
  335.     /**
  336.      * list all databases
  337.      *
  338.      * @param object    $db        database object that is extended by this class
  339.      * @return mixed data array on success, a MDB error on failure
  340.      * @access public
  341.      */
  342.     function listDatabases(&$db)
  343.     {
  344.         return($db->queryCol("SELECT table_name, tablespace_name FROM user_tables"));
  345.     }
  346.  
  347.     // }}}
  348.     // {{{ createSequence()
  349.  
  350.     /**
  351.      * create sequence
  352.      * 
  353.      * @param object $dbs database object that is extended by this class
  354.      * @param string $seq_name name of the sequence to be created
  355.      * @param string $start start value of the sequence; default is 1
  356.      * @return mixed MDB_OK on success, a MDB error on failure
  357.      * @access public 
  358.      */
  359.     function createSequence(&$db, $seq_name, $start)
  360.     {
  361.         $sequence_name = $db->getSequenceName($seq_name);
  362.         return($db->query("CREATE SEQUENCE $sequence_name START WITH $start INCREMENT BY 1".($start < 1 ? " MINVALUE $start" : '')));
  363.     }
  364.  
  365.     // }}}
  366.     // {{{ dropSequence()
  367.  
  368.     /**
  369.      * drop existing sequence
  370.      * 
  371.      * @param object $dbs database object that is extended by this class
  372.      * @param string $seq_name name of the sequence to be dropped
  373.      * @return mixed MDB_OK on success, a MDB error on failure
  374.      * @access public 
  375.      */
  376.     function dropSequence(&$db, $seq_name)
  377.     {
  378.         $sequence_name = $db->getSequenceName($seq_name);
  379.         return($db->query("DROP SEQUENCE $sequence_name"));
  380.     }
  381. }
  382.  
  383. };
  384. ?>